package com.cta.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cta.dao.SitePersistence;
import com.cta.entity.Contact;
import com.cta.exception.CTASystemException;

/**
 * @author son.nguyencong
 * 
 */
@Service("userCustomService")
@Transactional(readOnly = true)
public class UserAuthenticationServiceImpl implements UserDetailsService {

	@Autowired
	private SitePersistence sitePersistence;

	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		if (username == null) {
			throw new CTASystemException("Cannot retrieve null user Id.");
		}
		Contact user = sitePersistence.findSingleObject(Contact.class, "email",
				username);
		if (user == null) {
			throw new UsernameNotFoundException("No records with username "
					+ username);
		}
		return user;
	}

}
